import { Component, OnInit , Output, EventEmitter, Input} from '@angular/core';
import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../public/serve/init/init.service';
import { keyword } from '../../keyword.data';
import { appKeywordUrl } from '../../keyword.url';

declare var $:any;

@Component({
  selector: 'app-trial-tables',
  templateUrl: './trial-tables.component.html',
  styleUrls: ['./trial-tables.component.css']
})
export class TrialTablesComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();
  @Input() searchData: any = keyword.retrieval.queryCriteriaTable;

  tabListVal : Array<Object>;
  constructor(
    public request: CudTableService,
    private hintsService: HintsService,
    private server:Service
  ) { }

  isActive = false;
  themeTableId :any;
  
  isActiveLeft = this.server.isActive;
  ngOnInit() {
    this._tablesList();
    this.server.eventEmit.subscribe((value: any)=>{
      this.isActiveLeft = value;
    });
  }

  /**
   * 点击主题表
   * @param tid;
   */
  _clickTable(tid,tname){
    this.isActive = tid;    
    this.themeTableId = tid;
    keyword.retrieval.themeTableId = tid;
    keyword.retrieval.themeTableName = tname;
    this.onSubmit.emit();
  }

  /**
   * 查询主题词表列表
   */
  _tablesList(){
    let data = this._tablesData();
    this.request
    .postRequest(this.server.baseUrl + appKeywordUrl.getThemeTablesList,data)
    .then(res =>{
      if(res.result_code === "0000"){

        this.tabListVal = res.result;
        
        if(res.length > 0){
          if(keyword.retrieval.themeTableId){
            this.themeTableId = keyword.retrieval.themeTableId;
            this.isActive = false; 
          }else{
            this.themeTableId = this.tabListVal[0]['themeTableId'];
            keyword.retrieval.themeTableId = this.tabListVal[0]['themeTableId'];//默认设置第一条主题词表
            keyword.retrieval.themeTableName = this.tabListVal[0]['name'];
          }
          
          this.onSubmit.emit();
        }
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      return;
    })
    .catch(err =>{
      this.hintsService.messages("error","error");
      return;
    });
  }

  //搜索
  search() {
    keyword.retrieval.queryCriteriaTable = this.searchData;
    this._tablesList();
  }
  //响应搜索
  changesearch() {
      this.search();
  }

  _leftRetrieval(e){
      this.search();
  }

  //查询参数
  private _tablesData(){
    return {
      token         : keyword.retrieval.token,
      pageNo        : 1,
      pageSize      : 9999,
      queryCriteria : keyword.retrieval.queryCriteriaTable
    }
  }

}
